package com.personal.controller;

import com.personal.pojo.Result;
import com.personal.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        //判断文件是否为空
        if(file == null || file.isEmpty()) {
            log.info("文件为空");
            return Result.error("文件为空");
        }
        log.info("文件上传：{}",file.getOriginalFilename());
        //将文件交给oss存储
        String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
        log.info("文件上传成功，url：{}",url);
        return Result.success(url);
    }
}
